package com.example.service;


import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    // 用户登录认证
    public String authenticate(String username, String password) {
        // 从数据库查询用户
        User user = userMapper.findByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户名不存在");
        }

        // 验证密码是否匹配
        if (!(user.getPassword().equals(password))) {
            throw new RuntimeException("密码错误");
        }

        // 生成 JWT Token
        return jwtTokenUtil.generateToken(user.getUsername());
    }
}

